	<?php
if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
/**
 * short message service
 *
 * @author leebaotong
 *
 */
class Sms {

	/**
	 * 签名前缀
	 *
	 * @var string
	 */
	private $suffix = "";
	/**
	 * 签名
	 *
	 * @var string
	 */
	private $sign = "";
	/**
	 * 发送消息头
	 * @var string
	 */
	private $regHeader = "";
	/**
	 * 发送消息尾
	 * @var string
	 */
	private $regTail = "";
	private $sn = "";
	private $pwd = "";
	private $rrid = "";
	private $CI;

	/**
	 */
	public function __construct() {

		$this->CI = & get_instance ();

		/**
		 * 短信平台配置
		 */
		{
			$this->CI->config->load ( 'sms_config' );
			$this->regHeader = $this->CI->config->item ( 'reg_header' );
			$this->regTail = $this->CI->config->item ( 'reg_tail' );
			$this->suffix = $this->CI->config->item ( 'surffix' );
			$this->sign = $this->CI->config->item ( 'sign' );
			$this->sn = $this->CI->config->item ( 'sn' );
			$this->pwd = $this->CI->config->item ( 'pwd' );
			$this->rrid = $this->CI->config->item ( 'rrid' );
		}
	}
	
	/**
	 * [发送短信]
	 * @author fans
	 * @date 2016-7-20
	 **/
	public function send( $mobile , $content )
	{
		return $this->sendSMS( $mobile , 0 ,2 , $content);
	}
	
	/**
	 *
	 * @param number $tel
	 *        	手机号,使用此方法前自行验证;多个用英文的逗号隔开;
	 *        	post理论没有长度限制.推荐群发一次小于等于10000个手机号
	 * @param number $code
	 *        	验证码
	 * @param number $type
	 *        	类型(1注册，2发送自定义消息)
	 * @param string $msg
	 *        	信息
	 * @param string $suffix
	 *        	suffix是否保留“疑问”
	 * @return boolean
	 */
	public function sendSMS($tel = 0, $code = 0, $type = 1, $msg = '', $suffix = true) {
		$send_time = time();
		if (! $tel && ! $code) {
			return false;
		}
		$flag = 0;
		if ($type == 2) {
			$suffix = $suffix ? $this->suffix : $this->sign;
			$msg .=$suffix;
			$argv = array (
					'sn' => $this->sn, // //替换成您自己的序列号
					'pwd' => strtoupper ( md5 ( $this->sn . $this->pwd ) ),
					'mobile' => $tel, //
					'content' => iconv ( 'UTF-8', 'gb2312', $msg ),
					'ext' => '',
					//'stime' => date ( 'Y-m-d H:i:s' ),
					'stime' => "", // 定时时间,注意格式
					'rrid' => $this->rrid
			);
		} else {
			$msg=$this->regHeader.$code.$this->regTail;
			$argv = array (
					'sn' => $this->sn, // //替换成您自己的序列号
					'pwd' => strtoupper ( md5 ( $this->sn . $this->pwd ) ),
					'mobile' => $tel,
					//'content'=>iconv('UTF-8','gb2312',''.$code.''),
					'content' => iconv ( 'UTF-8', 'gb2312', $msg ),
					'ext' => '',
					//'stime' => date ( 'Y-m-d H:i:s' ), // 定时时间,注意格式
					'stime' => "", // 定时时间,注意格式
					'rrid' => $this->rrid
			);
		}

		// 构造要post的字符串
		$params = "";
		foreach ( $argv as $key => $value ) {
			if ($flag != 0) {
				$params .= "&";
				$flag = 1;
			}
			$params .= $key . "=";
			$params .= urlencode ( $value );
			$flag = 1;
		}
		$length = strlen ( $params );
		// 创建socket连接
		$fp = fsockopen ( "sdk2.entinfo.cn", 8060, $errno, $errstr, 10 ) or exit ( $errstr . "--->" . $errno );
		// 构造post请求的头
		$header = "POST /webservice.asmx/mt HTTP/1.1\r\n";
		$header .= "Host:sdk2.entinfo.cn\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-Length: " . $length . "\r\n";
		$header .= "Connection: Close\r\n\r\n";
		// 添加post的字符串
		$header .= $params . "\r\n";
		// 发送post的数据
		fputs ( $fp, $header );
		$inheader = 1;
		while ( ! feof ( $fp ) ) {
			$line = fgets ( $fp, 1024 ); // 去除请求包的头只显示页面的返回数据
			if ($inheader && ($line == "\n" || $line == "\r\n")) {
				$inheader = 0;
			}
		}
		$line = str_replace ( "<string xmlns=\"http://tempuri.org/\">", "", $line );
		$line = str_replace ( "</string>", "", $line );
		$result = explode ( "-", $line );
		$reback_time =time();
		//$this->log($tel, $type, $line, $send_time, $reback_time,$msg);
		if (count ( $result ) > 1) {
			return false;
		} else {
			return true;
		}
	}
	

	/**
	 * 生成随机字符串
	 *
	 * @param number $length
	 *        	截取字符串的长度,
	 * @param string $chars
	 *        	字符集(从此中抽取随机数)，可任意添加你需要的字符
	 * @return string 生成的随机字符串
	 */
	public function code($length = 6, $chars = "0123456789") {
		$password = '';
		for($i = 0; $i < $length; $i ++) {
			$password .= $chars [mt_rand ( 0, strlen ( $chars ) - 1 )];
		}
		return $password;
    }
    	/**
	 * [错误提示码]
	 * @param  [type] $code [description]
	 */
	public function errcode($code){
		$arr = array('-1','-12','-999','-2','-10','-11','-12','-13','-14','-101','-102','-103','-200','-999','-10001','-10003','-10011','-10029','-10030','-10031','-10057','-10056');
		if(in_array($code,$arr)){
			return false;
		}
		return true;
	}

}
